package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.WorkTypeObject;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.ManualWorkOrderService;
import com.tbit.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：hzw
 * @date ：Created in 2021/9/2 19:26
 * @description：手动工单类型设置
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@RequestMapping(value = "/manualWorkOrder")
public class ManualWorkOrderController {

    @Autowired
    private ManualWorkOrderService manualWorkOrderService;

    /**
     * 工单类型详情
     *
     * @return
     */
    @PostMapping(value = "/queryPage")
    public JsonResult queryPage(WorkTypeObject workType, @RequestParam(defaultValue = "1") Integer pageNO,
                                @RequestParam(defaultValue = "10") Integer rowCount) {

        List<WorkTypeObject> list = manualWorkOrderService.queryPage(workType.getAccountId(), pageNO, rowCount);

        Integer count = manualWorkOrderService.queryCount(workType.getAccountId());

        return JsonResult.ok(list, count);
    }

    /**
     * 工单类型增加
     *
     * @return
     */
    @PostMapping(value = "/insertWorkType")
    @OperateLog(module = ModuleEnum.MANUALWORKORDER, methods = MethodEnum.MANUALWORKORDER_ADD)
    public JsonResult insertWorkType(WorkTypeObject workType) {

        checkLength(workType);
        Integer count = manualWorkOrderService.insertType(workType);
        if (count == 0) {
            throw new BaseException(SystemCodeEnum.ADD_FAIL);
        }
        return JsonResult.ok();
    }

    /**
     * 工单类型修改
     *
     * @return
     */
    @PostMapping(value = "/updateWorkType")
    @OperateLog(module = ModuleEnum.MANUALWORKORDER, methods = MethodEnum.MANUALWORKORDER_UPDATE)
    public JsonResult updateWorkType(WorkTypeObject workType) {
        Assert.notNull(workType.getWorkType(), "ver.workType.notNull");
        checkLength(workType);
        Integer count = manualWorkOrderService.updateWorkType(workType);
        return JsonResult.ok();
    }

    /**
     * 工单类型删除
     *
     * @return
     */
    @PostMapping(value = "/delWorkType")
    @OperateLog(module = ModuleEnum.MANUALWORKORDER, methods = MethodEnum.MANUALWORKORDER_DEL)
    public JsonResult delWorkType(WorkTypeObject workType) {
        Assert.notNull(workType.getWorkType(), "ver.workType.notNull");
        manualWorkOrderService.delWorkType(workType);
        return JsonResult.ok();
    }

    void checkLength(WorkTypeObject workTypeObject){
        if (StrUtil.isNotEmpty(workTypeObject.getWorkName()) && workTypeObject.getWorkName().length() > 50) {
            throw new BaseException("工单名称过长");
        }
    }


}
